import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
// @ts-ignore
import cookie from "js-cookie";
import {getSysUserInfo} from "@/api/login";
import {message} from "ant-design-vue";

const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        name: 'home',
        component: () => import('@/views/home.vue'),
        meta: {isAuth: true, title: '首页'},
        redirect: '/data/five-cube',
        children: [
            {
                path: "data/five-cube",
                name: 'five-cube',
                component: () => import('@/views/visualization/five-cube.vue'),
                meta: {isAuth: true, title: '数据可视化'},
            },
            {
                path: "user",
                name: 'user',
                component: () => import('@/views/user/user/index.vue'),
                meta: {isAuth: true, title: '普通用户'},
            },
            {
                path: "sysUser",
                name: 'sysUser',
                component: () => import('@/views/user/sysUser/index.vue'),
                meta: {isAuth: true, title: '系统用户'},
            },
            {
                path: "problem",
                name: 'problem',
                component: () => import('@/views/answer/problem/index.vue'),
                meta: {isAuth: true, title: '问题管理'},
            },
            {
                path: "article",
                name: 'article',
                component: () => import('@/views/article/index.vue'),
                meta: {isAuth: true, title: '文章管理'},
            },
            {
                path: "report",
                name: 'report',
                component: () => import('@/views/answer/report/index.vue'),
                meta: {isAuth: true, title: '举报问题'},
            },
            {
                path: "activity",
                name: 'activity',
                component: () => import('@/views/activity/activity/index.vue'),
                meta: {isAuth: true, title: '活动管理'},
            },
            {
                path: "voucher/:id/:name",
                name: 'voucher',
                component: () => import('@/views/activity/voucher/index.vue'),
                meta: {isAuth: true, title: '优惠券'},
            },
            {
                path: "voucherDetails/:id",
                name: 'voucherDetails',
                component: () => import('@/views/activity/voucherDetails/index.vue'),
                meta: {isAuth: true, title: '抢购详情'},
            },
            {
                path: "payment",
                name: 'payment',
                component: () => import('@/views/payment/aliPay/index.vue'),
                meta: {isAuth: true, title: '支付管理'},
            },
        ]
    },
    {
        path: "/login",
        name: 'login',
        component: () => import('@/views/login/index.vue'),
        meta: {isAuth: false, title: '登录'},
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes: routes,
})

//前置路由守卫
router.beforeEach((to, from, next) => {
    //是否需要鉴权
    if (to.meta.isAuth) {
        if (cookie.get("sysToken") != null) {
            getSysUserInfo().then((res) => {
                //将用户信息存到cookie中
                cookie.set("sysUser", JSON.stringify(res.data.data.sysUser))
                next();
            })
        } else {
            message.warn("未登录，无权限查看!！").then(r => {
                return r
            })
            router.push("/login").then(r => {
                return r
            })
        }
    } else {
        next();
    }
})

//后置路由守卫
router.afterEach((to) => {
    document.title = to.meta.title?.toString() || ''
})

export default router